﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginInterFace;
using PluginInterFace.BLL;

namespace PickExperts2.form
{
    public partial class fmPicker : Form
    {
        private delegate void LogAppendDelegate(Color color, string text);
        private int rednum = 0;
        private int bluenum = 0;

        public fmPicker()
        {
            InitializeComponent();
        }

        private void LogAppend(Color color, string text)
        {
            richTextBox1.SelectionColor = color;
            richTextBox1.AppendText(text);
        }

        private void fmPicker_Load(object sender, EventArgs e)
        {
            Label lb;
            System.Drawing.Drawing2D.GraphicsPath path;
            foreach (var a in panel1.Controls)
            {
                if(a is Label)
                {
                    lb = (Label) a;
                    if (lb.Tag != null)
                    {
                        path = new System.Drawing.Drawing2D.GraphicsPath();
                        path.AddEllipse(lb.ClientRectangle);
                        lb.Region = new Region(path);
                        lb.Click += new System.EventHandler(this.label_click);
                    }
                }
            }
            //
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf";
            saveFileDialog1.Title = "保存号码列表";
            saveFileDialog1.FilterIndex = 0;
            Class.IniFile cIniFile = new Class.IniFile(Class.main.iniFPath);
            if (cIniFile.ReadInivalue("PickExperts2", "Picker_Check").ToLower() == "true")
                checkBox1.Checked = true;
            textBox1.Text = cIniFile.ReadInivalue("PickExperts2", "Picker_Red").ToLower();
            textBox2.Text = cIniFile.ReadInivalue("PickExperts2", "Picker_Blue").ToLower();
            textBox3.Text = cIniFile.ReadInivalue("PickExperts2", "Picker_Min").ToLower();
            textBox4.Text = cIniFile.ReadInivalue("PickExperts2", "Picker_Max").ToLower();
        }

        private void label_click(object sender, EventArgs e)
        {
            ChangeColor(sender);
        }

        private void ChangeColor(object sender)
        {
            Label lb = (Label)sender;
            PluginInterFace.BLL.picker bp = new PluginInterFace.BLL.picker();
            if (lb.Tag.ToString() == "red")
            {
                if (lb.BackColor == Color.Red)
                {
                    lb.BackColor = Color.White;
                    rednum--;
                }
                else
                    if (rednum + 1 > 16)
                    {
                        MessageBox.Show("最多只能选择16个红球", "消息");
                        return;
                    }
                    else
                    {
                        lb.BackColor = Color.Red;
                        rednum++;
                    }
            }
            else
            {
                if (lb.BackColor == Color.Blue)
                {
                    lb.BackColor = Color.White;
                    bluenum--;
                }
                else
                {
                    lb.BackColor = Color.Blue;
                    bluenum++;
                }
            }
            //计算注数
            if (rednum > 6 && bluenum > 0)
            {
                label50.Text = string.Format("已选择{0}注,共{1}元", bp.betCount(rednum) * bluenum, bp.betCount(rednum) * bluenum * 2);
            }
            else if (rednum == 6 && bluenum > 0)
            {
                label50.Text = string.Format("已选择{0}注,共{1}元", 1 * bluenum, 1 * bluenum * 2);
            }
            else
            {
                label50.Text = "已选择0注,共0元";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text.Length > 0)
            {
                //保存列表
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    switch (Path.GetExtension(saveFileDialog1.FileName))
                    {
                        case ".txt"://保存为文本文件
                            richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                            break;
                        case ".rtf"://保存为rtf文件
                            richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
                            break;
                        default:
                            return;
                    }
                    MessageBox.Show("保存完成!", "消息");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Label lb;
            string tmp;
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            if (rednum < 6 || bluenum < 1)
            {
                MessageBox.Show("至少选择6个红球和1个蓝球!", "消息");
                return;
            }
            tmp = "";
            foreach (var a in panel1.Controls)
            {
                if (a is Label)
                {
                    lb = (Label) a;
                    if (lb.BackColor == Color.Red)
                        tmp += lb.Text.PadLeft(2, '0') + " ";
                }
            }
            richTextBox1.Invoke(la, Color.Red, sortStr(tmp));
            tmp = "";
            foreach (var a in panel1.Controls)
            {
                if (a is Label)
                {
                    lb = (Label)a;
                    if (lb.BackColor == Color.Blue)
                        tmp += lb.Text.PadLeft(2, '0') + " ";
                }
            }
            richTextBox1.Invoke(la, Color.Blue, ":" + sortStr(tmp) + "\r\n");
        }

        private string sortStr(string str)
        {
            string tmp = str.Trim();
            string[] a = tmp.Split(' ');
            byte[] b = new byte[a.Length];
            for(int i=0;i<a.Length;i++)
                b[i] = byte.Parse(a[i]);
            Array.Sort(b);
            tmp = "";
            for (int i = 0; i < b.Length; i++)
                tmp += b[i].ToString().PadLeft(2,'0') + " ";
            return tmp.Trim();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int i, j, n;
            long x;
            PluginInterFace.BLL.picker bp = new PluginInterFace.BLL.picker();
            PluginInterFace.BLL.ssq bs = new PluginInterFace.BLL.ssq();
            List<int> list = new List<int>();
            string tmp;
            string[] strtmp;
            textBox5.Text = "";
            progressBar1.Minimum = 0;
            progressBar1.Maximum = (int)numericUpDown1.Value - 1;
            label56.Text = "号码生成中....";
            if (textBox3.Text != "" && textBox4.Text != "" && int.Parse(textBox3.Text) > int.Parse(textBox4.Text))
            {
                MessageBox.Show("和值限制填写错误", "消息");
                return;
            }
            string[] a = textBox1.Text.Split(',');
            if(a.Length<6)
            {
                MessageBox.Show("红球数据错误", "消息");
                return;
            }
            a = textBox2.Text.Split(',');
            if (a.Length < 1)
            {
                MessageBox.Show("蓝球数据错误", "消息");
                return;
            }
            loadingCircle1.Active = true;
            for (i = 0, x = 0; i < numericUpDown1.Value && x <= 17721088; i++, x++)
            {
                list = bp.GetRandomArray(textBox1.Text, 6);
                bp.Sort(list);
                list.Add(bp.GetRandomArray(textBox2.Text, 1)[0]);
                if (checkBox1.Checked && bs.ExistsData(list[0], list[1], list[2], list[3], list[4], list[5], list[6]))
                {//历史上出现过
                    i--;
                    continue;
                }
                tmp = "";
                for (j = 0; j < list.Count; j++)
                {
                    if (j == 5)
                        tmp += list[j].ToString().PadLeft(2, '0') + ":";
                    else
                        tmp += list[j].ToString().PadLeft(2, '0') + " ";
                }
                if (textBox5.Text.IndexOf(tmp) >= 0)
                {//列表中出现过
                    i--;
                    continue;
                }
                if (textBox3.Text != "" && textBox4.Text != "")
                {//和值限制
                    //strtmp = tmp.Trim().Replace(':', ' ').Split(' ');
                    strtmp = tmp.Trim().Substring(0, tmp.Trim().LastIndexOf(':')).Split(' ');
                    //MessageBox.Show(strtmp[0] + strtmp[1]+strtmp[2]+strtmp[3]+strtmp[4]+strtmp[5]);
                    n = 0;
                    for (int m = 0; m < strtmp.Length; m++)
                    {
                        n += int.Parse(strtmp[m]);
                    }
                    if (n < int.Parse(textBox3.Text) || n > int.Parse(textBox4.Text))
                    {
                        i--;
                        continue;
                    }
                }
                Application.DoEvents();
                progressBar1.Value = i;
                label56.Text = string.Format("号码生成中({0}/{1})....", i + 1, numericUpDown1.Value);
                textBox5.Text += tmp + "\r\n";
            }
            label56.Text = "号码生完毕";
            loadingCircle1.Active = false;
            MessageBox.Show("号码生成完毕", "消息");
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            textBox1.Text =
                "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33";
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            textBox2.Text = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16";
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ssq_pick ssp = new ssq_pick();
            PluginInterFace.Model.ssq_pick[] ssm;
            string[] tmp;
            string 期号 = "";
            if(richTextBox1.Text!="")
            {
                期号 = control.InputBox.ShowInputBox("输入期号:", "期号:", DateTime.Now.Year.ToString());
                if (期号 != "")
                {
                    tmp = richTextBox1.Lines;
                    ssm = new PluginInterFace.Model.ssq_pick[tmp.Length];
                    for(int i=0;i<tmp.Length;i++)
                    {
                        ssm[i] = new PluginInterFace.Model.ssq_pick();
                        ssm[i].期号 = int.Parse(期号);
                        ssm[i].号码 = tmp[i];
                    }
                    if (ssp.AddData(ssm))
                        MessageBox.Show("添加成功","消息");
                    else
                        MessageBox.Show("添加失败", "消息");
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string[] tmp, strtmp;
            int i;
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            if (textBox5.Text != "")
            {
                strtmp = textBox5.Lines;
                for (i = 0; i < strtmp.Length; i++)
                {
                    if (strtmp[i] == "" || strtmp[i] == "\r\n" || strtmp[i].Length < 10)
                        continue;
                    tmp = strtmp[i].Split(':');
                    richTextBox1.Invoke(la, Color.Red, tmp[0]);
                    richTextBox1.Invoke(la, Color.Blue, ":" + tmp[1] + "\r\n");
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Label lb;
            foreach (var a in panel1.Controls)
            {
                if (a is Label)
                {
                    lb = (Label)a;
                    if (lb.Tag != null)
                    {
                        lb.BackColor = Color.White;
                    }
                }
            }
            rednum = 0;
            bluenum = 0;
            label50.Text = "已选择0注,共0元";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            PluginInterFace.BLL.ssq bs = new PluginInterFace.BLL.ssq();
            string[] tmps;
            int[] tmp;
            if (textBox6.Text != "")
            {
                tmps = textBox6.Text.Split(' ');
                if(tmps.Length<6)
                    return;
                tmp = new int[tmps.Length];
                for (int i = 0; i < tmps.Length; i++)
                    tmp[i] = int.Parse(tmps[i]);
                if (checkBox1.Checked && bs.ExistsData(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6]))
                {
//历史上出现过
                    MessageBox.Show("此号码存在与开奖历史", "消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("此号码不存在与开奖历史", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }
}
